
package ma.crawler;

import ma.crawler.RegexConfig.Group;
import ma.util.FileUtil;

import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    public static class CmdConfig {

        public static final String IN_FILE = "-in-file";
        public static final String IN_TEXT = "-in-text";
        public static final String OUT_TEXT = "-out-text";
        public static final String OUT_DB = "-out-db";

        public String mInValue;
        public String mOutValue;

        public String mIn;
        public String mOut;

        public String mConfigPath;

        public String getInText() {
            if (IN_FILE.equalsIgnoreCase(mIn)) {
                return FileUtil.readFile(new File(mInValue));
            } else {
                return mInValue;
            }
        }

        public static CmdConfig init(JSONObject jsonObject) {
            CmdConfig config = new CmdConfig();
            JSONObject cmd = jsonObject.optJSONObject("cmd");
            config.mIn = cmd.optString("in");
            config.mInValue = cmd.optString("inargs");
            config.mOut = cmd.optString("out");
            config.mOutValue = cmd.optString("outargs");
            return config;
        }

        public static CmdConfig init(String[] args) {
            if (args == null || args.length == 0) {
                args = new String[] {
                        "-config", "D:\\config.txt",
                        "-in-file", "D:\\html.txt",
                        "-out-txt", "D:\\dest.txt",
                };
            }
            CmdConfig cmdConfig = new CmdConfig();
            cmdConfig.mConfigPath = args[1];
            cmdConfig.mIn = args[2];
            cmdConfig.mInValue = args[3];
            cmdConfig.mOut = args[4];
            cmdConfig.mOutValue = args[5];
            return cmdConfig;
        }
    }

    // -config $path -out-txt-f/-out-db-f $outpath -inf/-int-f
    // $inpath/txt
    public static void main(String[] args) {
        CmdConfig cmdConfig = CmdConfig.init(args);
        RegexConfig config = RegexConfig.initConfig(cmdConfig.mConfigPath);
        File outFile = new File(cmdConfig.mOutValue);
        config.printTitleToFile(outFile);

        Pattern pattern = Pattern.compile(config.mRegex);
        String text = cmdConfig.getInText();
        Matcher matcher = pattern.matcher(text);

        List<RegexConfig> outList = new ArrayList<RegexConfig>();

        List<Group> configGroups = config.mGroupList;

        while (matcher.find()) {
            RegexConfig out = new RegexConfig();
            List<Group> groupList = new ArrayList<Group>();
            for (Group g : configGroups) {
                Group group = new Group(g.mName, g.mIndex);
                group.setValue(matcher.group(g.mIndex));
                groupList.add(group);
            }
            out.mGroupList = groupList;
            out.mRegex = config.mRegex;
            outList.add(out);
        }
        outToFile(outList, outFile);
    }

    private static void outToFile(List<RegexConfig> outList, File outFile) {
        for (RegexConfig config : outList) {
            config.printValueToFile(outFile);
        }
    }
}
